package com.easidea.tim.flex.remote.inventory
{
	import com.easidea.tim.flex.Env;
	import com.easidea.tim.flex.bean.Page;
	import com.easidea.tim.flex.event.CustomerEvent;
	import com.easidea.tim.flex.remote.BaseDao;
	
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	public class StorageDao extends BaseDao
	{
		private var _service:RemoteObject = new RemoteObject();
		private static var _instance:StorageDao;
		
		
		public static function getInstance() : StorageDao {
			if ( _instance == null ){
				_instance = new StorageDao();
			}
			return _instance;
		}
		
		
		function StorageDao(){
			_service.channelSet = this.buildChannelSet();
			_service.destination = "storageService";
			_service.concurrency = "last";
			_service.makeObjectsBindable = true;
			_service.showBusyCursor = true;
		}
		
		
		public function searchProductTotally(productName:String, sampleFlag:Boolean, isIncludeChargeStorage:Boolean):void{
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.addEventListener(ResultEvent.RESULT, searchProductTotallyResult);
			_service.searchProductTotally(productName, sampleFlag, isIncludeChargeStorage);
		}
		
		private function searchProductTotallyResult(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,searchProductTotallyResult);
			Env.dispatchEvent(new CustomerEvent("searchProductTotallySuccess",e.result));
		}
		
		public function JudgeisCompleteProduct(productName:String,color:String,flowNo:String,meter:String):void{
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.addEventListener(ResultEvent.RESULT, isCompleteProductSuccess);
			_service.isCompleteProduct(productName,color,flowNo,meter);
		}
		
		private function isCompleteProductSuccess(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, isCompleteProductSuccess);
			Env.dispatchEvent(new  CustomerEvent("processCompleteStorageSuccess",e.result));
			
		}
		
		public function CancelComplete(productName:String,color:String,flowNo:String):void{
			_service.cancelChargedStorage(productName,color,flowNo);
		}
		
		public function CancelCompleteByFlowNo(flowNo:String):void{
			_service.cancelChargedStorageByFlowNo(flowNo);
		}
		
		
		public function pageStorageList(productName:String, color:String, sampleFlag:Boolean, page:Page, isIncludeChargeStorage:Boolean):void{
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.addEventListener(ResultEvent.RESULT, pageStorageListSuccess);
			_service.listProductStorage(productName,color,sampleFlag,page, isIncludeChargeStorage);
		}
		
		private function pageStorageListSuccess(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, pageStorageListSuccess);
			var page:Page = e.result as Page;
			if(page != null && page.totalCount == 0){
				Env.dispatchEvent(new CustomerEvent("pageStorageWholeListSuccess", e.result , "pageStorageWholeListSuccess"));
			}else{
				Env.dispatchEvent(new CustomerEvent("pageStorageListSuccess", e.result , "pageStorageListSuccess"));
			}
			
		}
		
		public function viewStorageDetail(pName:String,pColor:String,pSample:Boolean, chargeFlag:Boolean):void {
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.addEventListener(ResultEvent.RESULT, viewStorageDetailComplete);
			_service.listStorageDetail(pName,pColor,pSample,chargeFlag);
		}
		
		private function viewStorageDetailComplete(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT, viewStorageDetailComplete);
			Env.dispatchEvent(new CustomerEvent("viewDetailStorageSuccess",e.result));
			
		}
		
		public function listStorageBySaleOrder(id:int):void{
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.addEventListener(ResultEvent.RESULT, listStorageBySaleOrderResult);
			_service.listStorageBySaleOrder(id);
		}
		
		private function listStorageBySaleOrderResult(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,listStorageBySaleOrderResult);
			Env.dispatchEvent(new CustomerEvent("listStorageBySaleOrderSuccess", e.result));
		}
		
		public function getStorageSummary():void{
			_service.addEventListener(FaultEvent.FAULT, _Fault);
			_service.addEventListener(ResultEvent.RESULT, getStorageSummaryResult);
			_service.getStorageSummary();
		}
		
		private function getStorageSummaryResult(e:ResultEvent):void{
			_service.removeEventListener(ResultEvent.RESULT,getStorageSummaryResult);
			Env.dispatchEvent(new CustomerEvent("getStorageSummarySuccess", e.result, "getStorageSummarySuccess"));
		}
	}
}